package com.example.zhengsr.surfaceboard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.List;


public class DrawView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
    private static final String TAG = "DrawView";
    private Path mPath;
    private Paint mPaint;
    private boolean isDrawing;
    private Bitmap mBackBitmap;
    private  CommadTools mCommadTools;

    public DrawView(Context context) {
        this(context,null);
    }

    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setClickable(true);
        getHolder().addCallback(this);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        initTools();

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        mBackBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);





    }

    private void initTools() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);

        mPath = new Path();

        mCommadTools = CommadTools.create();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //阻止第一次进入黑屏的问题
        Canvas canvas = getHolder().lockCanvas();
        canvas.drawColor(Color.WHITE);
        getHolder().unlockCanvasAndPost(canvas);
        isDrawing = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;
    }

    private float mPreX,mPreY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mPreX = event.getRawX();
                mPreY = event.getRawY();
                mPath.moveTo(mPreX,mPreY);
                break;
            case MotionEvent.ACTION_MOVE:
                float x = event.getRawX();
                float y = event.getRawY();
                float dx = Math.abs(x - mPreX);
                float dy = Math.abs(y - mPreY);
                if (dx >2 || dy >2){ //有移动时才画
                    // 注意这里的mprex，虽然在down的时候，我们记录了mprex，但这里却是 move 的坐标
                    // 所以，这里的quadto，其实是在移动时坐标的中点
                    mPath.quadTo(mPreX,mPreY,(x + mPreX)/2,(y + mPreY)/2);
                }
                mPreX = x;
                mPreY = y;
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onTouchEvent(event);
    }


    private void drawing(){
        Canvas canvas = getHolder().lockCanvas();
        canvas.drawColor(Color.WHITE);

        canvas.drawPath(mPath, mPaint);

        getHolder().unlockCanvasAndPost(canvas);
    }

    public void reDraw(){

    }

    public void clear(){
        mPath.reset();
    }

    @Override
    public void run() {
        while (isDrawing) {
            drawing();
        }
    }
    public void undo() {
    }
}
